package com.example.demo.controller;

import com.example.demo.entity.BookDesc;
import com.example.demo.entity.BookInfo;
import com.example.demo.entity.Comment;
import com.example.demo.entity.User;
import com.example.demo.pojo.BSResult;
import com.example.demo.service.BookDescService;
import com.example.demo.service.BookInfoService;
import com.example.demo.service.CommentService;
import com.example.demo.service.UserService;
import com.example.demo.utils.BSResultUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.PageInterceptor;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/book")
public class BookInfoController {

    @Autowired
    private BookInfoService bookInfoService;

    @Autowired
    private CommentService commentService;

    @Autowired
    private UserService userService;

    @Autowired
    private BookDescService bookDescService;


    @RequestMapping("/info/{id}")
    public String bookInfo(@PathVariable("id") Integer id, Model model){

        BookInfo bookInfo=bookInfoService.findByBookId(id);

        List<Comment> commentList=commentService.selectCommentByBookId(id);

        for(Comment comment:commentList){

            User user=userService.findUserById(comment.getUser_id());
            comment.setImageUrl(user.getImageUrl());

        }

        BookDesc bookDesc=bookDescService.findBookDescById(id);
        model.addAttribute("bookDesc",bookDesc);



        PageHelper.startPage(1,15);
        List<BookInfo> recommendBoookList=bookInfoService.selectByCatId(bookInfo.getBook_category_id());

        model.addAttribute("commentList",commentList);
        model.addAttribute("bookInfo",bookInfo);
        model.addAttribute("recommendBookList",recommendBoookList);
        return "book_info";
    }

    @RequestMapping("/list")
    public String bookList(@RequestParam(defaultValue = "0",name="cate_id",required = false)int cate_id,
                           @RequestParam(defaultValue = "",name="keywords",required = false)String keywords,
                           @RequestParam(defaultValue = "1",name="page",required = false)int page,
                           @RequestParam(defaultValue = "6",name="pageSize",required = false)int pageSize,
                           Model model){


        PageInfo<BookInfo> bookPageInfo=bookInfoService.findByCondition(cate_id,keywords,page,pageSize,0);
        model.addAttribute("bookPageInfo",bookPageInfo);
        model.addAttribute("keywords", keywords);
        model.addAttribute("cate_id", cate_id);
        return "book_list";
    }




    }



